package com.autotest.glin.dao;

import org.apache.log4j.Logger;

import java.io.IOException;
import java.sql.*;

public class CRUDData {
    static Logger log = Logger.getLogger(CRUDData.class);
    static Connection connection = null;

    public static ResultSet selectAllData(String sql) throws SQLException {
        connection = DBConnection.getConnection();
        Statement statement = connection.createStatement();
        ResultSet result = statement.executeQuery(sql);
        if (connection != null)
            connection.close();
        return result;
    }

    /*
     * 查询 只针对select a from talbe 类型的语句； select * from 或 select a,b from等暂时用不到，暂不支持；
     */
    public static String selectOneData(String sql) throws SQLException, IOException, ClassNotFoundException {
        String value = "";

        connection = DBConnection.getConnection();
        Statement statement = connection.createStatement();
        ResultSet result = statement.executeQuery(sql);

        if (result.next()) { // 如果存在多个值，只取第一个
            value = result.getString(1);
        }
        log.info(sql + "取值为：" + value);

        if (result != null)
            result.close();
        if (connection != null)
            connection.close();

        return value;
    }

    // 新增，修改，删除
    public static void cUDData(String sql, String flag) throws SQLException, IOException, ClassNotFoundException {

        if (flag.toLowerCase().equals("db"))
            connection = DBConnection.getConnection();
        else
            connection = DBConnection.getConnection();

        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        preparedStatement.executeUpdate();

        log.info("新增/修改/删除语句：" + sql);

        if (connection != null) {
            preparedStatement.close();
            connection.close();
        }

    }
}
